﻿#include "tinydiskclient.h"
#include "ui_tinydiskclient.h"
#include <QHostAddress>
#include <utils.h>
#include <QIcon>
#include "operatewidget.h"
#include <QMutex>

TinyDiskClient *TinyDiskClient::getInstance()
{
    static QMutex mutex;
    static QScopedPointer<TinyDiskClient> inst;
    if (Q_UNLIKELY(!inst)) {
        mutex.lock();
        if (!inst) {
            inst.reset(new TinyDiskClient);
        }
        mutex.unlock();
    }
    return inst.data();
}

TinyDiskClient::TinyDiskClient(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::TinyDiskClient)
{
    ui->setupUi(this);
    this->setWindowIcon(QIcon(":/img/tinyDiskDisConnected.png"));
    this->setWindowTitle("登录(未连接服务器)");

    m_tcpSocket = ClientDiskTcpSocket::getInstance();
}

TinyDiskClient::~TinyDiskClient()
{
    delete ui;
}

void TinyDiskClient::handle_ConnectToHost(bool isConnected)
{
    // 连接上了
    if(isConnected){
        this->setWindowIcon(QIcon(":/img/tinyDisk.png"));
        this->setWindowTitle("登录");
    }
    //断开连接
    else{
        this->setWindowIcon(QIcon(":/img/tinyDiskDisConnected.png"));
        this->setWindowTitle("登录(未连接服务器)");
    }
}

void TinyDiskClient::handle_Regist_Respond(PDU* pduRecv)
{

    if(strcmp(pduRecv->caData, REGIST_OK) == 0){
        QMessageBox::information(this,"注册","用户注册成功！");
    }else{
        QMessageBox::warning(this,"注册",pduRecv->caData);
    }
}

void TinyDiskClient::handle_Login_Respond(PDU* pduRecv)
{
    if(strcmp(pduRecv->caData,LOGIN_OK) == 0){
        // QMessageBox::information(this,"登录","用户登录成功！");
        OperateWidget::getInstance().setUsrName(m_strLoginName);
        OperateWidget::getInstance().show();
        // 刷新一次文件
        OperateWidget::getInstance().getFileWidget()->flushFiles();
        this->hide();
    }else{
        QMessageBox::information(this,"登录","用户登录失败！请检查用户名或者密码是否正确以及是否已经登录！");
    }
}

void TinyDiskClient::handle_disconnectHost()
{
    QMessageBox::information(TinyDiskClient::getInstance(),"提示","服务器断开连接请重新登录！");

    OperateWidget::getInstance().hide();
    TinyDiskClient::getInstance()->show();
}

void TinyDiskClient::on_login_pbn_clicked()
{
    QString usrName = ui->usrName_lb->text();
    QString pwd = ui->pwd_lb->text();
    if(!pwd.isEmpty() && !usrName.isEmpty()){
        m_strLoginName = usrName;
        ClientDiskTcpSocket::getInstance()->setUsrName(m_strLoginName);
        ClientDiskTcpSocket::getInstance()->setStrCurPath(ROOT_PATH + m_strLoginName);
        PDU* pdu = mkPDU(0);
        pdu->uiMsgType = ENUM_MSG_TYPE_LOGIN_REQUEST;
        memcpy(pdu->caData,usrName.toStdString().c_str(), 32);
        memcpy(pdu->caData + 32 ,pwd.toStdString().c_str(),32);
        MYLOG<<m_tcpSocket->write((char*)pdu,pdu->uiPDULen);
        free(pdu);
        pdu = NULL;
    }else{
        QMessageBox::critical(this,"登录失败","用户名或密码为空！");
    }
}


void TinyDiskClient::on_register_pbn_clicked()
{
    QString usrName = ui->usrName_lb->text();
    QString pwd = ui->pwd_lb->text();
    if(!pwd.isEmpty() && !usrName.isEmpty()){
        PDU* pdu = mkPDU(0);
        pdu->uiMsgType = ENUM_MSG_TYPE_REGIST_REQUEST;
        memcpy(pdu->caData,usrName.toStdString().c_str(), 32);
        memcpy(pdu->caData + 32 ,pwd.toStdString().c_str(),32);
        MYLOG<<m_tcpSocket->write((char*)pdu,pdu->uiPDULen);
        free(pdu);
        pdu = NULL;
    }else{
        QMessageBox::critical(this,"注册失败","用户名或密码为空！");
    }
}


void TinyDiskClient::on_cancel_pbn_clicked()
{

}

